import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      component: () => import('../views/Layout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'dashboard',
          component: () => import('../views/Dashboard.vue'),
          meta: { title: '控制台' }
        },
        {
          path: 'health-data',
          name: 'healthData',
          component: () => import('../views/health/HealthDataList.vue'),
          meta: { title: '健康数据' }
        },
        {
          path: 'health-data/add',
          name: 'healthDataAdd',
          component: () => import('../views/health/HealthDataForm.vue'),
          meta: { title: '添加健康数据' }
        },
        {
          path: 'exercise-records',
          name: 'exerciseRecords',
          component: () => import('../views/exercise/ExerciseRecordList.vue'),
          meta: { title: '运动记录' }
        },
        {
          path: 'exercise-records/add',
          name: 'exerciseRecordAdd',
          component: () => import('../views/exercise/ExerciseRecordForm.vue'),
          meta: { title: '添加运动记录' }
        },
        {
          path: 'exercise-records/edit/:id',
          name: 'exerciseRecordEdit',
          component: () => import('../views/exercise/ExerciseRecordForm.vue'),
          meta: { title: '编辑运动记录' }
        },
        {
          path: 'exercise-types',
          name: 'exerciseTypes',
          component: () => import('../views/exercise/ExerciseTypeList.vue'),
          meta: { title: '运动类型管理', adminOnly: true }
        },
        {
          path: 'fitness-goals',
          name: 'fitnessGoals',
          component: () => import('../views/goals/FitnessGoalList.vue'),
          meta: { title: '健身目标' }
        },
        {
          path: 'fitness-goals/add',
          name: 'fitnessGoalAdd',
          component: () => import('../views/goals/FitnessGoalForm.vue'),
          meta: { title: '添加健身目标' }
        },
        {
          path: 'fitness-goals/edit/:id',
          name: 'fitnessGoalEdit',
          component: () => import('../views/goals/FitnessGoalForm.vue'),
          meta: { title: '编辑健身目标' }
        },
        {
          path: 'profile',
          name: 'profile',
          component: () => import('../views/user/Profile.vue'),
          meta: { title: '个人资料' }
        },
        {
          path: 'statistics',
          name: 'statistics',
          component: () => import('../views/statistics/Statistics.vue'),
          meta: { title: '数据统计' }
        },
        {
          path: 'rankings',
          name: 'rankings',
          component: () => import('../views/rankings/RankingsList.vue'),
          meta: { title: '健身排行榜' }
        },
        {
          path: 'user-management',
          name: 'userManagement',
          component: () => import('../views/user/UserManagement.vue'),
          meta: { title: '用户管理', adminOnly: true }
        },
        {
          path: 'user-exercise-management',
          name: 'userExerciseManagement',
          component: () => import('../views/user/UserExerciseManagement.vue'),
          meta: { title: '用户健身数据管理', adminOnly: true }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFound.vue')
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const store = useUserStore()
  const hasToken = store.token
  const userRole = store.userRole
  
  // 不需要认证的页面直接放行
  if (to.meta.requiresAuth === false) {
    next()
    return
  }
  
  // 需要认证但没有token，跳转到登录页
  if (to.meta.requiresAuth !== false && !hasToken) {
    next({ name: 'login' })
    return
  }
  
  // 管理员权限控制
  if (userRole === 'ADMIN') {
    // 管理员只能访问运动类型管理、健身排行榜、用户管理、用户健身数据管理和个人资料页面
    const adminAllowedPaths = ['/exercise-types', '/rankings', '/profile', '/user-management', '/user-exercise-management']
    // 如果不在允许访问的路径列表中，重定向到运动类型管理页面
    if (!adminAllowedPaths.some(path => to.path.startsWith(path))) {
      next({ path: '/exercise-types' })
      return
    }
  } 
  // 非管理员访问管理员专属页面
  else if (to.meta.adminOnly) {
    next({ name: 'dashboard' })
    return
  }
  
  // 其他情况放行
  next()
})

export default router
